﻿function setHighlight(keyword, selectRange) {
    var range;
    if (keyword == "") return;
    if (selectRange) {
        range = jDoc(selectRange)
    } else {
        range = document.body.getAttribute("highlight");
    }
    if (range) {
        var eles = range.getElementsByTagName("*"), value = "";
        for (var i = 0, len = eles.length; i < len; i++) {
            value = eles[i].getAttribute("highlight");
            var keywordList = keyword.replace(/\s+/gi, " ").split(" ");
            var data = "";
            if (value == "true") {
                data = eles[i].innerHTML;
                for (var k = 0, l = keywordList.length; k < l; k++) {
                    data = data.replace(keywordList[k], "<font color=\"red\">" + keywordList[k] + "</font>");
                }
                eles[i].innerHTML = data;
            }
        }
    }
}

function setHighlightTag(range, keywords, color) {
    var oRange;
    if (keywords.replace(/\s+/gi, "") == "") return;
    if (range) { oRange = range; }
    else if (document.body) { oRange = document.body; }
    else { return; }
    keywords = keywords.replace(/\s+/gi, " ");
    setHighlightTag.keywordList = keywords.split(" ");
    if (setHighlightTag.isFirst == false) {
        clearHighlightTag(range, keywords);
    } else {
        setHighlightTag.isFirst = true;
    }
    var nodes = oRange.childNodes, node;
    for (var i = 0, len = nodes.length; i < len; i++) {
        node = nodes[i];
        if (node.nodeType == 1) { arguments.callee(node, keywords, color) }
        else if (node.nodeType == 3 && node.parentNode.style.display != "none") {
            var data = node.data, pNode = node.parentNode;
            if (containsHighlightKeywords(data)) {
                if (pNode.parentNode && pNode.parentNode.getAttribute("highlight") == "true") {
                    pNode.style.color = color;
                } else {
                    AddTagToHighlightKeywords(data, node, pNode);
                }
            }
        }
    }
}
setHighlightTag.isFirst = false;
setHighlightTag.keywordList = [];
function containsHighlightKeywords(data) {
    var keywordList = setHighlightTag.keywordList;
    for (var i = 0, len = keywordList.length; i < len; i++) {
        var reg = new RegExp("(" + keywordList[i] + ")", "gi");
        if (reg.test(data)) {
            return true;
        }
    }
    return false;
}
function AddTagToHighlightKeywords(data, node, pNode) {
    var keywordList = setHighlightTag.keywordList;
    var index = 0;
    var eleH = document.createElement("span");
    eleH.setAttribute("highlight", "true");
    for (var i = 0, len = keywordList.length; i < len; i++) {
        var reg = new RegExp("(" + keywordList[i] + ")", "gi");
        if (reg.test(data)) {
            data = data.replace(reg, "<span class=\"keywd\">$1</span>");
            index++;
        }
    }
    if (index > 0) {
        eleH.innerHTML = data;
        pNode.replaceChild(eleH, node);
    }
}

function clearHighlightTag(range, keywords) {
    var oRange;
    if (range) { oRange = range; }
    else if (document.body) { oRange = document.body; }
    else { return; }
    var nodes = range.childNodes, node, pNode;
    for (var i = 0, len = nodes.length; i < len; i++) {
        node = nodes[i];
        if (node.nodeType == 1) {
            if (node.getAttribute("highlight") == "true") {
                pNode = node.parentNode;
                var textNode = document.createTextNode("");
                textNode.data = getHighlightKeywords(node);
                pNode.replaceChild(textNode, node);
            } else {
                arguments.callee(node, keywords);
            }
        }
    }
}
function getHighlightKeywords(ele) {
    var text = "";
    if (ele.innerText) {
        text = ele.innerText;
    } else if (ele.textContent) {
        text = ele.textContent;
    } else {
        text = ele.innerHTML.replace(/<[^>]+>/gi, "");
    }
    return text;
}